package com.fyzk.activity.user.coupon;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.bean.CouponListBean;
import com.tyjc.lottery.R;
import com.fyzk.utils.ComparatorValues;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created by fanxs11 on 17/1/23.
 *
 * 支付界面优惠券选择
 */
public class CouponZhifuView extends LinearLayout {

    private Context context;

    private ImageView icon;
    private TextView tishi;
    private TextView tishi2;
    private TextView zhanKai;
    private LinearLayout gv_ll;
    private GridView gv;
    private CouponZhifuGridViewAdaptr adapter;
    public List<CouponListBean.CouponsBean> couponListInfos = new ArrayList<>();

    private long couponMoney = 0;   // 优惠彩金
    private String couponId = "";

    private OnCheckCouponListener listener;

    private long money = 0;

    private LinearLayout coupon_ll;
    private TextView refresh_tv;

    public CouponZhifuView(Context context, AttributeSet attrs, int defStyle, OnCheckCouponListener listener) {
        super(context, attrs, defStyle);

        this.context = context;
        this.listener = listener;
        initView();
    }

    public CouponZhifuView(Context context, AttributeSet attrs, OnCheckCouponListener listener) {
        this(context, attrs, 0, listener);
    }

    public CouponZhifuView(Context context, OnCheckCouponListener listener) {
        this(context, null, listener);
    }

    private void initView() {
        View.inflate(context, R.layout.coupon_zhifu_view, this);
        icon = findViewById(R.id.coupon_zhifu_iv);
        tishi = findViewById(R.id.coupon_zhifu_tishi);
        tishi2 = findViewById(R.id.coupon_zhifu_tishi2);
        zhanKai = findViewById(R.id.coupon_zhifu_zhankai);
        gv_ll = findViewById(R.id.coupon_zhifu_gv_ll);
        gv = findViewById(R.id.coupon_zhifu_gv);
        adapter = new CouponZhifuGridViewAdaptr();
        gv.setAdapter(adapter);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });

        coupon_ll = findViewById(R.id.coupon_zhifu_ll);
        refresh_tv = findViewById(R.id.coupon_zhifu_refresh_tv);

        zhanKai.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (gv_ll.getVisibility() == View.GONE) {
                    gv_ll.setVisibility(View.VISIBLE);
                    zhanKai.setText("收起优惠券");

                    if (adapter != null && adapter.getCount() > 6){
                        ViewGroup.LayoutParams params = gv_ll.getLayoutParams();
                        params.height = 366;
                        gv_ll.setLayoutParams(params);
                    }

                } else {
                    gv_ll.setVisibility(View.GONE);
                    zhanKai.setText("展开优惠券");
                }
            }
        });
    }

    public void setList(List<CouponListBean.CouponsBean> couponListInfos){
        this.couponListInfos = couponListInfos;

        Collections.sort(this.couponListInfos, new ComparatorValues());


        if (couponListInfos.size() == 0) {
            this.setVisibility(View.GONE);
        } else {
            this.setVisibility(View.VISIBLE);
            tishi.setText("此订单满"+couponListInfos.get(0).getOrderamt()+"米可优惠"+couponListInfos.get(0).getDisamt()+"米");
            if (this.money > 0) {
                setMoney(this.money);
            }
        }
        adapter.notifyDataSetChanged();
    }

    public void setMoney(long money) {
        couponId = "";
        couponMoney = 0;
        boolean isC = false;
        this.money = money;
        for (int i = 0; i < couponListInfos.size(); i++) {
            CouponListBean.CouponsBean info = couponListInfos.get(i);
            if (info.getOrderamt() <= money) {
                info.isUse = true;

            } else {
                info.isUse = false;
                info.isChecked = false;
            }

            if (info.isChecked) {
                isC = true;
                couponId = info.getId();
                try {
                    couponMoney = info.getDisamt();
                } catch (Exception e) {

                }
            }
        }

        if (couponListInfos.size() > 0) {
            if (couponListInfos.get(0).isUse) {
                if (!isC) {
                    couponListInfos.get(0).isChecked = true;
                    couponId = couponListInfos.get(0).getId();
                    try {
                        couponMoney = couponListInfos.get(0).getDisamt();
                    } catch (Exception e) {

                    }
                    tishi.setText("现可用满"+couponListInfos.get(0).getOrderamt()+"米优惠"+couponListInfos.get(0).getDisamt()+"米");

                    boolean isTishi2 = false;
                    for (int j = 0; j < couponListInfos.size(); j++) {
                        int order1 = couponListInfos.get(0).getOrderamt();
                        int order2 = couponListInfos.get(j).getOrderamt();

                        if (order2 > order1){
                            tishi2.setText("如满"+couponListInfos.get(j).getOrderamt()+"米可优惠"+couponListInfos.get(j).getDisamt()+"米");
                            isTishi2 = true;
                            break;
                        }
                    }
                    if (!isTishi2) {
                        tishi2.setText("");
                    }
                }
            } else {
                tishi.setText("此订单满"+couponListInfos.get(0).getOrderamt()+"米可优惠"+couponListInfos.get(0).getDisamt()+"米");
                tishi2.setText("");
            }

        }

        if (gv_ll.getVisibility() == View.GONE) {
            if (listener != null) {
                listener.checkCoupon(couponMoney, couponId);
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 获取数据失败
     */
    public void netError() {
        coupon_ll.setVisibility(GONE);
        refresh_tv.setVisibility(GONE); // 不显示刷新按钮，获取失败不显示优惠券..
    }

    /**
     * 刷新数据
     * @param listener
     */
    public void setOnRefreshData(OnClickListener listener) {
        refresh_tv.setOnClickListener(listener);
    }

    public long getCouponMoney() {
        return couponMoney;
    }

    public String getCouponId() {
        return couponId;
    }

    class CouponZhifuGridViewAdaptr extends BaseAdapter {

        @Override
        public int getCount() {
            return couponListInfos.size();
        }

        @Override
        public Object getItem(int i) {
            return couponListInfos.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup) {

            final CouponListBean.CouponsBean info = couponListInfos.get(i);
            final HolderView holderView;
            if (view == null) {
                holderView = new HolderView();
                view = LayoutInflater.from(context).inflate(R.layout.coupon_zhifu_item, null);
                holderView.bg_ll = view.findViewById(R.id.coupon_zhifu_item_ll);
                holderView.disMoney_tv = view.findViewById(R.id.coupon_zhifu_dismoney_tv);
                holderView.name_tv = view.findViewById(R.id.coupon_zhifu_item_name);
                holderView.content_tv = view.findViewById(R.id.coupon_zhifu_item_content);
                holderView.endTime_tv = view.findViewById(R.id.coupon_zhifu_item_time);
                holderView.isCheck_iv = view.findViewById(R.id.coupon_zhifu_item_ischeck_iv);
                view.setTag(holderView);
            } else {
                holderView = (HolderView) view.getTag();
            }

            holderView.disMoney_tv.setText(info.getDisamt()+"");
            holderView.name_tv.setText(info.getTitle());
            holderView.content_tv.setText("满"+info.getOrderamt()+"可使用");
            holderView.endTime_tv.setText(info.getEndDate());

            holderView.isCheck_iv.setVisibility(View.GONE);


            if (info.isUse) {
                holderView.bg_ll.setBackgroundResource(R.drawable.coupon_zhifu_bg);
            } else {
                holderView.bg_ll.setBackgroundResource(R.drawable.coupon_zhifu_bg_no);
            }

            if (info.isChecked) {
                holderView.isCheck_iv.setVisibility(View.VISIBLE);
                couponId = info.getId();
                try {
                    couponMoney = info.getDisamt();
                } catch (Exception e) {

                }
                tishi.setText("现可用满"+couponListInfos.get(i).getOrderamt()+"米优惠"+couponListInfos.get(i).getDisamt()+"米");
                boolean isTishi2 = false;
                for (int j = i; j < couponListInfos.size(); j++) {
                    int order1 = couponListInfos.get(i).getOrderamt();
                    int order2 = couponListInfos.get(j).getOrderamt();

                    if (order2 > order1){
                        tishi2.setText("如满"+couponListInfos.get(j).getOrderamt()+"米可优惠"+couponListInfos.get(j).getDisamt()+"米");
                        isTishi2 = true;
                        break;
                    }
                }
                if (!isTishi2) {
                    tishi2.setText("");
                }
                if (listener != null) {
                    listener.checkCoupon(couponMoney, couponId);
                }
            } else {
                holderView.isCheck_iv.setVisibility(View.GONE);
            }

            holderView.bg_ll.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (info.isUse) {
                        for (int j = 0; j < couponListInfos.size(); j++) {
                            CouponListBean.CouponsBean info = couponListInfos.get(j);
                            if (j != i){
                                info.isChecked = false;
                            }
                        }
                        info.isChecked = !info.isChecked;
                        if (info.isChecked) {
                            holderView.isCheck_iv.setVisibility(View.VISIBLE);
                            couponId = info.getId();
                            try {
                                couponMoney = info.getDisamt();

                            } catch (Exception e) {

                            }
                        } else {
                            holderView.isCheck_iv.setVisibility(View.GONE);
                            couponId = "";
                            couponMoney = 0;
                            tishi.setText("此订单满"+couponListInfos.get(0).getOrderamt()+"米可优惠"+couponListInfos.get(0).getDisamt()+"米");
                            tishi2.setText("");
                        }

                        if (listener != null) {
                            listener.checkCoupon(couponMoney, couponId);
                        }

                        notifyDataSetChanged();
                    } else {
                        AppToast.getToast().show("彩金不够，增加倍数试试看");
                    }
                }
            });
            return view;
        }

        class HolderView {
            LinearLayout bg_ll;
            TextView disMoney_tv;
            TextView name_tv ;
            TextView content_tv;
            TextView endTime_tv;
            ImageView isCheck_iv;
        }

    }

    public interface OnCheckCouponListener {
        public void checkCoupon(long couponMoney, String couponId);
    }
}
